<template>
  <div>
    <el-card>
      <div slot="header" class="clearfix">
        <el-input
          placeholder="请输入内容"
          v-model="input"
          class="inputStyle"
          @keyup.enter.native="EmployeeQuery"
        >
          <li
            slot="prefix"
            class="el-input__icon el-icon-search"
            @click="EmployeeQuery"
          />
        </el-input>
        <el-button class="buttonStyle" @click="addEmployee"
          >+新增员工</el-button
        >
      </div>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column 
          prop="name" 
          label="员工姓名" 
        />
        <el-table-column 
          prop="username" 
          label="账号" 
        />
        <el-table-column 
          prop="phone" 
          label="手机号" 
        />
        <el-table-column 
          prop="status" 
          label="账号状态"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.status === 1 ? "正常" : "禁用" }}</span>
          </template>
        </el-table-column>
        <el-table-column 
          label="操作" 
          class="lastStyle"
          width="100"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="editEmployee(scope.row.id)"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="small"
              style="color: #ff0000"
              @click="statusHandle(scope.row)"
            >
              {{ scope.row.status == "1" ? "禁用" : "启用" }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageStyle">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { getMemberList, AbleEmployee } from "@/api/employee";
export default {
  data() {
    return {
      tableData: [],
      page: 1,
      pageSize: 10,
      input: "",
      id: "",
      status: "",
      total: 0,
    };
  },
  created() {
    this.fetchData();
    this.statusHandle();
  },
  methods: {
    async fetchData() {
      const changeData = {
        page: this.page,
        pageSize: this.pageSize,
        name: this.input ? this.input : undefined,
      };
      await getMemberList(changeData)
        .then((res) => {
          console.log(res);
          this.tableData = res.data.records;
          this.total = res.data.total;
        })
        .catch((err) => {
          this.$message.error("请求出错了：" + err);
        });
    },
    EmployeeQuery() {
      this.page = 1;
      this.fetchData();
    },
    addEmployee() {
      this.$router.push({
        path: "/editEmployee",
        query: {
          action: "ADD",
        },
      });
    },
    statusHandle(row) {
      this.id = row.id;
      this.status = row.status;
      this.$confirm("是否确认调整该账号的状态?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        AbleEmployee({
          id: this.id,
          status: !this.status ? 1 : 0,
        }).then((res) => {
          this.$message.success("账号状态更改成功！");
          this.fetchData();
        });
      })
    },
    editEmployee(id) {
      this.$router.push({
        path: "/editEmployee",
        query: {
          action: "EDIT",
          id,
        },
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.fetchData();
    },
  },
};
</script>

<style lang="scss" scoped>
.clearfix {
  position: relative;
  display: flex;
  width: 100%;
  .inputStyle {
    margin-top: 20px;
    margin-left: 20px;
    width: 20%;
  }
  .buttonStyle {
    position: absolute;
    right: 20px;
    top: 20px;
    background-color: pink;
    width: 160px;
    height: 50px;
    font-size: 20px;
    border: none;
  }
  .buttonStyle:hover {
    color: white;
  }
}
.pageStyle {
  margin: 0 auto;
  padding-top: 80px;
  padding-bottom: 20px;
  text-align: center;
}
</style>
